package com.zhidian.edm.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zhidian.edm.entity.SysRoleMenuPO;
import com.zhidian.edm.entity.SysRolePO;
import com.zhidian.edm.entity.SysUserRolePO;
import com.zhidian.edm.mapper.SysRoleMapper;
import com.zhidian.edm.mapper.SysRoleMenuMapper;
import com.zhidian.edm.mapper.SysUserRoleMapper;
import com.zhidian.edm.param.AddRoleParam;
import com.zhidian.edm.param.UpdateRoleMenuParam;
import com.zhidian.edm.service.ISysMenuService;
import com.zhidian.edm.service.ISysRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhidian.edm.util.BeanConvertUtil;
import com.zhidian.edm.util.ListUtil;
import com.zhidian.edm.vo.SysMenuVO;
import com.zhidian.edm.vo.SysRoleVO;
import com.zhidian.edm.vo.SysUserVO;
import jakarta.annotation.Resource;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sl
 * @since 2025-09-18
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRolePO> implements ISysRoleService {

    @Resource
    private SysRoleMapper sysRoleMapper;

    @Resource
    private SysRoleMenuMapper sysRoleMenuMapper;

    @Resource
    private SysUserRoleMapper sysUserRoleMapper;


    @Override
    public List<SysRoleVO> getRoleList() {
        List<SysRolePO> sysRolePOS = sysRoleMapper.selectList(null);
        return BeanConvertUtil.convertList(sysRolePOS, SysRoleVO.class);
    }

    @Override
    public SysRoleVO getRoleDetailById(Long roleId) {
        SysRolePO sysRolePO = sysRoleMapper.selectOne(new LambdaQueryWrapper<>(SysRolePO.class).eq(SysRolePO::getId, roleId));
        SysRoleVO result = BeanConvertUtil.convert(sysRolePO, SysRoleVO.class);
        if(Objects.nonNull(result)){
            List<SysRoleMenuPO> sysRoleMenuPOS = sysRoleMenuMapper.selectList(new LambdaQueryWrapper<>(SysRoleMenuPO.class).eq(SysRoleMenuPO::getRoleId, roleId));
            if(CollectionUtil.isNotEmpty(sysRoleMenuPOS)){
                List<Long> valueList = ListUtil.toValueList(sysRoleMenuPOS, SysRoleMenuPO::getMenuId);
                result.setCheckedKeys(valueList);
            }
        }

        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteRoleById(Long roleId) {
        int i = sysRoleMapper.deleteById(roleId);
        if (i <= 0) {
            throw new RuntimeException("删除角色失败");
        }
        sysRoleMenuMapper.delete(new LambdaQueryWrapper<>(SysRoleMenuPO.class).eq(SysRoleMenuPO::getRoleId, roleId));
        sysUserRoleMapper.delete(new LambdaQueryWrapper<>(SysUserRolePO.class).eq(SysUserRolePO::getRoleId, roleId));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateRoleAndMenuAuth(UpdateRoleMenuParam param) {
        if(Objects.isNull(param)){
            return;
        }
        Long roleId = param.getId();
        List<Long> checkedKeys = param.getCheckedKeys();
        sysRoleMenuMapper.delete(new LambdaQueryWrapper<>(SysRoleMenuPO.class).eq(SysRoleMenuPO::getRoleId, roleId));
        if(CollectionUtil.isNotEmpty(checkedKeys)){
            List<SysRoleMenuPO> sysRoleMenuPOS = checkedKeys.stream().map(menuId -> {
                SysRoleMenuPO sysRoleMenuPO = new SysRoleMenuPO();
                sysRoleMenuPO.setRoleId(roleId);
                sysRoleMenuPO.setMenuId(menuId);
                return sysRoleMenuPO;
            }).toList();
            sysRoleMenuMapper.insert(sysRoleMenuPOS);
        }

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addRole(AddRoleParam param) {
        if(Objects.isNull(param)){
            throw new IllegalArgumentException("角色参数不能为空");
        }
        SysRolePO role = new SysRolePO();
        BeanConvertUtil.copyProperties(param, role);
        int insertCount = sysRoleMapper.insert(role);
        if (insertCount <= 0) {
            throw new RuntimeException("新增角色失败");
        }
        if (CollectionUtil.isNotEmpty(param.getMenuIds())) {
            List<SysRoleMenuPO> roleMenus = param.getMenuIds().stream()
                    .map(menuId -> {
                        SysRoleMenuPO roleMenu = new SysRoleMenuPO();
                        roleMenu.setRoleId(role.getId());
                        roleMenu.setMenuId(menuId);
                        return roleMenu;
                    })
                    .collect(Collectors.toList());

            // 批量插入
            sysRoleMenuMapper.insert(roleMenus);
        }
    }
}
